package me.dr0n3.skyRanks.Commands;

import me.dr0n3.skyRanks.Utils.IOHelper;
import me.dr0n3.skyRanks.Utils.TimeHelper;
import me.dr0n3.skyRanks.skyRanks;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
 * @author dr0n3
 * @date 20.04.2012
 */
public class playedCommand implements CommandExecutor
{
    skyRanks plugin;
    
    public playedCommand(skyRanks instance)
    {
        plugin = instance;
    }
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) 
    {
        if(!(sender instanceof Player))
            return false;
        
        Player player = (Player) sender;
        
        if(args.length == 0)
            {
                long old = IOHelper.loadPlaytime(player);
                long now = System.currentTimeMillis();
                long login = TimeHelper.hmLoginTime.get(player);
                
                long act = now - login;
                act += old;
                
                player.sendMessage(ChatColor.YELLOW + "Deine Spielzeit: " + TimeHelper.MsToTime(act));
                return true;
            }
            else if(args.length == 1)
            {
                long old = IOHelper.loadPlaytime(player, args[0]);
                if(old == 0)
                    player.sendMessage(ChatColor.RED + "Die Spielzeit von " + args[0] + " wurde nicht gefunden. (case-sensitive?)");
                else
                {
                    if(TimeHelper.hmLoginTime.containsKey(player))
                    {
                        long act2 = System.currentTimeMillis() - TimeHelper.hmLoginTime.get(player);
                        act2 += old;
                        player.sendMessage(ChatColor.YELLOW + args[0] + "'s Spielzeit: " + TimeHelper.MsToTime(act2));
                    }
                    else
                        player.sendMessage(ChatColor.RED + args[0] + "'s Spielzeit: " + old);
                }
                
                return true;
            }
            else
                return false;
    }
    
}
